//
//  YPChoicePacketTableViewCell.m
//  ChengHuiTong
//
//  Created by ypwl on 16/8/6.
//  Copyright © 2016年 张文权. All rights reserved.
//

#import "YPChoicePacketTableViewCell.h"
#import "NSDate+Extension.h"
#import "AppTools.h"
@implementation YPChoicePacketTableViewCell{
    
    
    UILabel*_money,*_useSpace,*_endTime;
    
    UIButton*_backGroundButton;
}

- (void)awakeFromNib {
    // Initialization code
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        //590/120
        
        if ([reuseIdentifier isEqualToString:@"cell"]) {
            _backGroundButton=[UIButton buttonWithType:UIButtonTypeCustom];
            _backGroundButton.frame=CGRectMake(10, 5, SCREEN_WIDTH-20, (SCREEN_WIDTH-30)*120/590.0);
            [_backGroundButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:_backGroundButton];
            
            
            _money = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, _backGroundButton.width/3.0-20, _backGroundButton.height-25)];
            _money.textColor = [UIColor whiteColor];
            _money.font = [UIFont systemFontOfSize:12];
            [_backGroundButton addSubview:_money];
            
            
            _useSpace = [[UILabel alloc] initWithFrame:CGRectMake(_backGroundButton.width/3.0+10, 10, _backGroundButton.width/2.0-15, 10)];
            _useSpace.textColor = [UIColor whiteColor];
            _useSpace.font = [UIFont systemFontOfSize:10];
            _useSpace.numberOfLines=3;
            [_backGroundButton addSubview:_useSpace];
            
            
            _endTime = [[UILabel alloc] initWithFrame:CGRectMake(_useSpace.left, _useSpace.bottom+5, _useSpace.width, _useSpace.height)];
            _endTime.textColor = _useSpace.textColor;
            _endTime.font = [UIFont systemFontOfSize:10];
            [_backGroundButton addSubview:_endTime];

        }else if ([reuseIdentifier isEqualToString:@"cell1"]){
            
            UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
            button.frame=CGRectMake(20, 15, SCREEN_WIDTH-40, 35);
            [button setBackgroundImage:[AppTools imageWithSize:button.size borderColor:UIColorFromString(@"#565656") borderWidth:1] forState:0];
            [button setTitle:@"不使用" forState:0];
            [button setTitleColor:UIColorFromString(@"#565656") forState:0];
             [button addTarget:self action:@selector(UnUsebuttonClick) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:button];
            
        }
        
    }
    
    return self;
    
}

-(void)UnUsebuttonClick{
    
    if (self.UnUserredBagClick) {
        
        self.UnUserredBagClick();
        
            }

}

-(void)buttonClick:(UIButton*)button{
    
    if (!_model.canUse) {
        
        return;
    }
    
    [self cellSetColorWithBool:YES];
    
    if (self.redBagClick) {
        
        self.redBagClick(_model,button);
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    UIFont*font;
    
    if (SCREEN_WIDTH<375) {
        
        font=[UIFont systemFontOfSize:24];
    }else{
        
        font=[UIFont systemFontOfSize:36];
    }
    
    if (_model.apr && _model.apr.length > 0) {
        NSMutableAttributedString*string=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%%",_model.apr]];
        [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length-1)];
        _money.attributedText=string;
    }else{
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.numberStyle = NSNumberFormatterRoundFloor;
        NSString*newApr = [formatter stringFromNumber:[NSNumber numberWithFloat:[_model.money floatValue]]];
        NSArray*arr=[[NSString stringWithFormat:@"￥%@",newApr] componentsSeparatedByString:@"."];
        NSMutableAttributedString*string=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@",newApr]];
        NSString*str=arr[0];
        [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(1, str.length-1)];
        
        _money.attributedText=string;
    }

    _money.textColor=UIColorFromString(@"#565656");
    
    _useSpace.textColor=UIColorFromString(@"#565656");
    
    _endTime.textColor=UIColorFromString(@"#565656");
    
    CGSize size=[_model.remark sizeWithFont:[UIFont systemFontOfSize:10] andMaxSize:CGSizeMake(_useSpace.width, 1000)];
    _useSpace.text=_model.remark;
    _useSpace.height=size.height;
    
    _endTime.top=_useSpace.bottom+5;
    _endTime.text=[NSString stringWithFormat:@"使用期限:%@",[NSDate getDateWithInterval:[_model.end_time doubleValue] formart:@"yyyy-MM-dd HH:mm"]];
    if (!_model.canUse) {
        [_backGroundButton setBackgroundImage:[UIImage imageNamed:@"不可用红包"] forState:0];
    }
}


-(void)cellSetColorWithBool:(BOOL)isChoice{
    
    
    if (isChoice) {
        
        _money.textColor=kColor_AppNavColor;
        
        _useSpace.textColor=kColor_AppNavColor;
        
        _endTime.textColor=kColor_AppNavColor;

        
    }else{
        
        _money.textColor=UIColorFromString(@"#565656");
        
        _useSpace.textColor=UIColorFromString(@"#565656");
        
        _endTime.textColor=UIColorFromString(@"#565656");

    }
    
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
